<?php

namespace backend\modules\supplier\controllers\goods;

use Yii;
use backend\modules\supplier\models\GoodsClass;
use backend\modules\supplier\models\GoodsClassMap;
use backend\modules\supplier\models\GoodsClassSearch;
use backend\modules\supplier\models\SupplierController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * GoodsClassController 继承自 GoodsClass 模型。
 */
class ClassController extends SupplierController {

    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 显示所有 GoodsClass 模型。
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new GoodsClassSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * 显示单个 GoodsClass 模型。
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * 创建一个 GoodsClass 模型。
     * 如果创建成功,浏览器将被重定向到“视图”页。
     * @return mixed
     */
    public function actionCreate() {
        $model = new GoodsClass();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $this->adminLog($model);
            
            $tree = GoodsClass::getTree();
            $content = $this->renderAjax('_sub', [
                'tree' => $tree,
                'is_first'=>0,
            ]);

            $this->renderJSON($content);
            
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * 更新现有的 GoodsClass 模型。
     * 如果更新成功,浏览器将被重定向到“视图”页。
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate() {
        $post = Yii::$app->request->post();

        $id = intval($post['id']);
        $model = $this->findModel($id);
        $post['type'] == 'name' && $model->name = $post['val'];
        $post['type'] == 'sort' && $model->sort = $post['val'];

        if ($model->save()) {
            $this->adminLog($model);

            $tree = GoodsClass::getTree();
            $content = $this->renderAjax('_sub', [
                'tree' => $tree,
                'is_first'=>0,
            ]);

            $this->renderJSON($content);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * 删除现有的 GoodsClass 模型。
     * 如果删除成功,浏览器将被重定向到“index”页面。
     * @param integer $id
     * @return mixed
     */
    public function actionDelete() {
        $post = Yii::$app->request->post();
        if (isset($post['id'])) {
            $model = $this->findModel($post['id']);

            //检查是否有子分类，如果有，则不允许删除
            $count = $model->find()->where(['parent_id' => $model->id])->count();
            if ($count > 0) {
                $this->renderJSON([ 'error_msg' => '该分类下含有子分类，无法删除！'],false);
            }

            //删除分类映射关系
            GoodsClassMap::deleteAll(['goods_class_id' => $model->id]);

            $model->delete() && $this->adminLog($model);

            $tree = GoodsClass::getTree();
            
            $content = $this->renderAjax('_sub', [
                'tree' => $tree,
                'is_first'=>0,
            ]);

            $this->renderJSON($content);
        }
    }

    /**
     * 根据主键ID，查找 GoodsClass 模型。
     * 如果模型没有找到，则会抛出一个404异常。
     * @param integer $id
     * @return GoodsClass the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = GoodsClass::findOne($id)) !== null && $model->supplier_id == Yii::$app->user->identity->supplier_id) {
            return $model;
        } else {
            throw new NotFoundHttpException('所请求的页面不存在。');
        }
    }

}
